

<div style="padding: 5px;text-align: right">
    <button class="layui-btn" onclick="sub()">添加</button>
    <table id="demo" lay-filter="test"></table>
</div>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script>
    var myTable;
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null)
            return decodeURI(r[2]);
        return null;
    }
    var storeList = [];
    layui.use(['form','table','laydate'], function(){
        var table = layui.table,form = layui.form;

        //第一个实例
        myTable = table.render({
            elem: '#demo'
            ,height: 'full-160'
            ,url: 'admin/getShoptype'
            ,cols: [[ //表头
                {field: 'name', title: '分类名', align:'center',edit:"text"},
                {title: '操作', align:'center', toolbar: '#barDemo'}
            ]]
            ,page:false
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event;

            if(layEvent === 'del'){
                layer.confirm('确定执行操作吗？', function(index){

                    layer.close(index);
                    //向服务端发送删除指令
                    $.post("admin/delShoptype","id="+data.id,function (data) {
                        if (data.code==1){
                            obj.del(); //删除对应行（tr）的DOM结构
                        }

                        layer.msg(data.msg);
                    });
                });
            }
        });


        //监听单元格编辑
        table.on('edit(test)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            $.post("admin/editShoptype","id="+data.id+"&key="+field+"&value="+value,function (data) {
                layer.msg(data.msg);
            });
        });

        form.render();

    });
    function sub() {
        var no = "";
        var money = 0;
        layer.prompt({"title":"请输入分类名"},function(val, index){
            money = val;
            layer.close(index);
            $.post("admin/addShoptype","name="+val,function (data) {
                layer.msg(data.msg);
                myTable.reload();
            });
        });
    }






</script>
